<template>
  <div class="container">
    <a-spin size="large" :tip="tip">
      <div class="container"></div>
    </a-spin>
  </div>
</template>
<script lang="ts" setup>
import { useRoute } from "vue-router";
import LocalCache from "@/utils/cache";
import { APPID } from "@/service/request/config";
import {
  requestSysInfo,
  accountLoginRequest,
  getTokenAddOrgCode,
} from "@/service/modules/public/public";
import router from "@/router";
import { Toast } from "vant";
import CryptoJS from "crypto-js";
import qs from "qs";
document.title = "设备报修";
const md5 = require("md5");
const tip = "加载中...";
const route = useRoute();
const default_actk =
  "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI0NjcyODI2ODE1MTY0NzAyNzMiLCJpYXQiOjE3MTU4MzA0NjksImFjY291bnRObyI6ImxwNWJnIiwibG9naW5EZXZpY2UiOiJtaW5pYXBwIiwiZXhwIjoxNzE1OTE2ODY5fQ.EdcsM0hz4ykWITjyf-OLontpL7MeR64wFYi3yQYMjFB-1CrtjcodzJal5fURDS-g9NGpdh6lo-wVJFEQf5NYixzXh29qxL8Jdx1zKTo-Kt3YU5B-3BsFhjEl7ElP8dUwh65fGqOTfWGqp6p_A0wkVVXDGtx0gTwMabWfxI6rnrE";
getAppToken();
// 获取orgCode与token
/**
 * @desc: 初始化
 * @author: went
 */
async function initIdentity(_token: String, orgCode: String, personId: String) {
  const query = qs.parse(decodeURIComponent(route.fullPath.split("?")[1]));
  const params = {
    accessToken: query?.accessToken || default_actk,
    appId: APPID,
    studentId: query?.studentId || "",
    orgCode,
    token: _token,
    personId,
    menuListSource: 1,
  };
  const { code, token } = await accountLoginRequest(params);
  if (code === 0) {
    LocalCache.setCache("token", token);
    getSysInfo(query);
  }
}
/**
 * @desc: 获取用户信息
 * @author: went
 */
async function getSysInfo(query) {
  const { code, data } = await requestSysInfo();
  if (code === 0) {
    LocalCache.setSession("userInfo", data);
    if (query.msgId) {
      if (query.type === "repairor") {
        // 报修人查看详情
        router.replace(`/repair/detail?id=${query.msgId}&type=home`);
      } else {
        router.replace(`/repair/detail?id=${query.msgId}`);
      }
    } else {
      router.replace("/home");
    }
  } else {
    Toast.success(res?.message);
  }
}

/**
 * 计算appToken
 */
async function getAppToken() {
  LocalCache.clearCache();
  sessionStorage.clear();
  const query = qs.parse(decodeURIComponent(route.fullPath.split("?")[1]));
  const { accessToken = default_actk } = query;
  let appId = "JF-ISWCC-WEB";
  let appAuthorizeCode = "00003A32774441869306A38D38CCDA41";
  const timeStamp = new Date().getTime();
  const key = md5(timeStamp + "").slice(8, 24);
  const appToken = encrypt(appId + appAuthorizeCode + timeStamp, key);
  const {
    code,
    data: { token, orgCode, personId },
  } = await getTokenAddOrgCode(appToken, timeStamp, accessToken);
  if (code === "200") {
    LocalCache.setCache("orgToken", token);
    LocalCache.setCache("orgCode", orgCode);
    initIdentity(token, orgCode, personId);
  }
}

/**
 * 加密算法
 */
function encrypt(word, keyStr) {
  if (word && keyStr) {
    var key = CryptoJS.enc.Utf8.parse(keyStr); //Latin1 w8m31+Yy/Nw6thPsMpO5fg==
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.ciphertext.toString();
  } else {
    return false;
  }
}
// function getMenu() {
//   // store.dispatch("addRoutes/actionMenu", menuList);
//   getSysInfo();
// }
</script>

<style scoped lang="less">
.container {
  width: 100%;
  height: 100vh;
}
</style>
